package com.scs.application.config;

import com.alibaba.druid.filter.config.ConfigFilter;
import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Scope;

/**
 * druid 配置
 *
 */
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class DruidConfig {
//    @Value("${spring.datasource.dynamic.datasource.master.url}")
//    private String url;
//    @Value("${spring.datasource.dynamic.datasource.master.username}")
//    private String username;
//    @Value("${spring.datasource.dynamic.datasource.master.password}")
//    private String password;
//    @Value("${spring.datasource.dynamic.datasource.master.driver-class-name}")
//    private String driverClassName;

    /**
     * ===============================spring 监控  开始===============================
     */
    @Bean
    public DruidStatInterceptor druidStatInterceptor() {
        DruidStatInterceptor dsInterceptor = new DruidStatInterceptor();
        return dsInterceptor;
    }

    @Bean
    @Scope("prototype")
    public JdkRegexpMethodPointcut druidStatPointcut() {
        JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
        pointcut.setPatterns("com.scs.application.modules.*.mapper.*", "com.scs.application.modules.*.service.impl.*", "com.scs.application.core.service.*");
        return pointcut;
    }

    @Bean
    public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) {
        DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
        defaultPointAdvisor.setPointcut(druidStatPointcut);
        defaultPointAdvisor.setAdvice(druidStatInterceptor);
        return defaultPointAdvisor;
    }
    /**  ===============================spring 监控 结束 =============================== */


    /**
     * =============================== SQL 监控 开始 ===============================
     */


    @Bean
    public Slf4jLogFilter logFilter() {

        Slf4jLogFilter logFilter = new Slf4jLogFilter();
        logFilter.setStatementExecutableSqlLogEnable(true);
        logFilter.setStatementLogEnabled(false);
        return logFilter;
    }

    @Bean
    public StatFilter statFilter() {

        StatFilter statFilter = new StatFilter();
        statFilter.setSlowSqlMillis(3000);
        statFilter.setLogSlowSql(true);
        statFilter.setMergeSql(true);
        return statFilter;
    }

    /**
     * sql防火墙过滤器配置
     *
     * @param wallConfig
     * @return
     */
    @Bean
    public WallFilter wallFilter(WallConfig wallConfig) {

        WallFilter wallFilter = new WallFilter();
        wallFilter.setConfig(wallConfig);
        //对被认为是攻击的SQL进行LOG.error输出
        wallFilter.setLogViolation(true);
        //对被认为是攻击的SQL抛出SQLException
        wallFilter.setThrowException(true);
        return wallFilter;
    }

    /**
     * sql防火墙配置
     *
     * @return
     */
    @Bean
    public WallConfig wallConfig() {

        WallConfig wallConfig = new WallConfig();
        wallConfig.setAlterTableAllow(false);
        wallConfig.setCreateTableAllow(false);
        wallConfig.setDeleteAllow(true);
        wallConfig.setMergeAllow(false);
        wallConfig.setDescribeAllow(false);
        wallConfig.setShowAllow(false);
        wallConfig.setMultiStatementAllow(true);
        wallConfig.setNoneBaseStatementAllow(true);
        return wallConfig;
    }
    /**  =============================== SQL 监控 结束 =============================== */


    /**
     * @return
     */
    @Bean
    public ConfigFilter configFilter() {
        ConfigFilter configFilter = new ConfigFilter();

        return configFilter;
    }
}
